package cn.bdqn.controller.course;

import cn.bdqn.pojo.course.Course;
import cn.bdqn.pojo.course.CourseTag;
import cn.bdqn.pojo.course.CourseType;
import cn.bdqn.service.course.CourseTagService;
import cn.bdqn.service.course.CourseTypeService;
import cn.bdqn.utils.Json;
import com.alibaba.fastjson.JSONObject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.List;

@RestController
@RequestMapping("/api")
public class CourseTypeController {
    @Autowired
    private CourseTypeService courseTypeService;

    /**
     * 根据课程id获取该课程类型
     * @param courseId  课程id
     * @return  返回课程类型集合
     */
    @GetMapping("/selectCourseTypeByCourseId")
    public Object selectCourseTypeByCourseId(String courseId){
        //检查课程id是否为空
        if (null == courseId || courseId.trim().length() == 0) {
            return new Json(400,"课程id不能为空");
        }else{
            List<CourseType> courseTypes = courseTypeService.selectCourseTypeByCourseId(Integer.parseInt(courseId));
            return Json.isEmpty(courseTypes);
        }
    }

    /**
     * 添加课程类型
     * @param selectList    课程标签id集合
     * @param courseId  课程id
     * @return  返回json对象
     */
    @PreAuthorize("hasAuthority('course:management')")
    @PostMapping("/insertCourseType")
    public Object insertCourseType(@RequestParam("selectList") int [] selectList,@RequestParam("courseId")Integer courseId){
        //检查课程标签数组是否为空
        if (null == selectList || selectList.length == 0) {
            return new Json(400,"课程标签不能为空");
        }
        //检查课程id是否为空
        if (null == courseId || courseId <= 0) {
            return new Json(400,"课程id不能为空");
        }

        //将数组添加添加到课程类型集合对象中
        List<CourseType> courseTypes = new ArrayList<>();
        for (int courseTagId : selectList) {
            CourseType courseType = new CourseType();
            courseType.setCourseId(courseId);
            courseType.setCourseTagId(courseTagId);
            //添加到集合中
            courseTypes.add(courseType);
        }

        //添加到数据库中
        boolean result = courseTypeService.insertCourseTypeList(courseTypes);

        if (result) {
            return new Json(200,"添加成功");
        }else {
            return new Json(400,"添加课程标签失败");
        }

    }

    /**
     * 修改课程类型
     * @param courseSelectTags  修改后的课程标签数组
     * @param courseId  课程id
     * @return  返回json对象
     */
    @PreAuthorize("hasAuthority('course:management')")
    @PutMapping("/updateCourseTags")
    public Object updateCourseTags(@RequestParam("courseSelectTags") int[] courseSelectTags,@RequestParam("courseId") Integer courseId){

        Json json = null;
        //检查课程id是否为空
        if(null == courseId){
            json =  new Json(400,"课程id不能为空");
        }else{
            //检查标签是否为空
            if (null == courseSelectTags){
                json = new Json(400,"课程标签接收失败");
            }else {
                //删除原有标签
                Integer integer = courseTypeService.deleteCourseTypeByCourseId(courseId);
                if (integer >= 1) {
                    if(courseSelectTags.length >= 1){
                        //将数组添加添加到课程类型集合对象中
                        List<CourseType> courseTypes = new ArrayList<>();
                        for (int courseTagId : courseSelectTags) {
                            CourseType courseType = new CourseType();
                            courseType.setCourseId(courseId);
                            courseType.setCourseTagId(courseTagId);
                            //添加到集合中
                            courseTypes.add(courseType);
                        }

                        //添加到数据库中
                        boolean result = courseTypeService.insertCourseTypeList(courseTypes);
                        if (result) {
                            json = new Json(200,"更新成功");
                        } else {
                            json = new Json(400,"更新失败");
                        }
                    }else {
                        json = new Json(200,"更新成功");
                    }
                }else {
                    json = new Json(400,"更新失败");
                }
            }
        }

        return json;

    }
}
